package com.example.sharding.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.sharding.dao.PvStationMapper;
import com.example.sharding.enerty.PvStation;
import com.example.sharding.enerty.SysDictionary;
import com.example.sharding.service.PvStationService;
import com.example.sharding.service.SysDictionaryService;
import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CacheTest {
    @Resource
    PvStationMapper pvStationMapper;
    @Resource
    PvStationService pvStationService;

    @Resource
    SysDictionaryService sysDictionaryService;

    @Cacheable(value = "zhaoxiangbiao1", key = "'keyName11'")
    @GetMapping("/hello")
    @ResponseBody
    public String get(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("station_name","郭村幸福电站");
        HintManager.getInstance().setMasterRouteOnly();//从主数据库查询
        List list = pvStationMapper.selectList(queryWrapper);
        return list.toString();
    }
    //    @DictTypeSensibles({
//            @DictTypeSensible(value = "countyCode", code = "COUNTY_CODE"),
//    }
//    )
    @GetMapping("/getPage")
    @ResponseBody
    public IPage<SysDictionary> getPage(){
        IPage<SysDictionary> page = sysDictionaryService.getPage();
        return page;
    }


    @GetMapping("/add")
    public void add(){
        for (int i = 0; i < 30; i++) {
            PvStation pvStation = new PvStation();
            pvStation.setCityCode("10000"+i);
            pvStation.setCountyCode("20000"+i);
            pvStation.setProvinceCode("30000"+i);
            pvStation.setStationName("牛村发电站"+i);
            int insert = pvStationMapper.insert(pvStation);
        }

    }
    @GetMapping("/selectlist")
    public List<PvStation> selectlist(){
        QueryWrapper<PvStation> pvStationQueryWrapper = new QueryWrapper<>();
        pvStationQueryWrapper.like("station_name","牛村发电站");
        List<PvStation> pvStations = pvStationMapper.selectList(pvStationQueryWrapper);
        return pvStations;

    }
    /**
     * 批量插入
     */
    @GetMapping("/addBanch")
    @ResponseBody
    public void addBanch(){
        List<PvStation> l1 = new ArrayList<>();

        for (int i = 20; i <40 ; i++) {
            PvStation pvStation = new PvStation();
            pvStation.setCityCode("0000"+i);
            pvStation.setCountyCode("1000"+i);
            pvStation.setProvinceCode("2000"+i);
            pvStation.setStationName("羊村书记"+i);

            l1.add(pvStation);

        }
        pvStationService.saveBatch(l1);
    }
}
